// pages/category/category.js
import { require} from '../../require/index.js'
import  regeneratorRuntime from '../../lib/runtime/runtime.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    letMentList: [],
    rightContent: [],
    currentIndex: 0,
    scrollTop: 0
  },
  Cate: [],
  onLoad() {
    const cates = wx.getStorageSync('cates');
    if (!cates) {
      this.Cate()
    } else {
      if(Date.now()-cates.time>1000*10){
        this.Cate()
      } else {
        console.log('aa')
        this.Cate = cates.data
        let letMentList = this.Cate.map((i) => i.cat_name)
        let rightContent = this.Cate[0].children
        this.setData({
          letMentList: letMentList,
          rightContent: rightContent
        })
      }
    }
    
  },
  async Cate() {
    // 从本地缓存区数据
    // require({url: '/categories'})
    // .then(res =>{
    //   this.Cate = res.data.message
    //   // 把数据存到本地
    //   wx.setStorageSync('cates', {time: Date.now(), data: this.Cate});
    //   // 渲染数据
    //   let letMentList = this.Cate.map((i) => i.cat_name)
    //   let rightContent = this.Cate[0].children
    //   this.setData({
    //     letMentList: letMentList,
    //     rightContent: rightContent
    //   })
    // })
    // 使用es7 的async await 发送请求
    const res = await require({url: '/categories'})
    this.Cate = res
      // 把数据存到本地
      wx.setStorageSync('cates', {time: Date.now(), data: this.Cate});
      // 渲染数据
      let letMentList = this.Cate.map((i) => i.cat_name)
      let rightContent = this.Cate[0].children
      this.setData({
        letMentList: letMentList,
        rightContent: rightContent
      })
  },
  handdleItemTab(e) {
    console.log(e)
    // 获取被鼠标点击的索引
    // 给data中的currentIndex赋值  3改变右边显示rightContent
    const {index} = e.currentTarget.dataset
    let rightContent = this.Cate[index].children
    this.setData({
      currentIndex: index,
      rightContent,
      scrollTop: 0
    })
  }

})